let wxpay = require('../../utils/pay.js')
let app = getApp()
let NORMAL = 0, LOWER = 1, PULLDOWN = 2
Page({
  data:{
    statusType: ["申请售后", "处理中", "已处理"],
    currentType: 0,
    productList: [],
    imgUrl: app.globalData.imgUrl,
    offset: 1,
    limit: 10,
    loading: false,
    loadingComplete: false,
    loaded: false,
    noConnect: false,
  },
  statusTap: function(e){
     var curType =  e.currentTarget.dataset.index;
     this.setData({
       productList: [],
       currentType: curType
     });

    this.setData({
      offset: 1,
      limit: 10,
      loading: false,
      loadingComplete: false,
      loaded: false,
      noConnect: false,
    })

     this.onShow();
  },

  
  onLoad:function(e){
    // this.setData({
    //   currentType: e.type
    // })
    //this.onShow()
   
  },
  onReady:function(){
    // 生命周期函数--监听页面初次渲染完成
 
  },
  
  onShow:function(){
    this.getDataList(this.data.currentType, NORMAL)
  },
  onHide:function(){
    // 生命周期函数--监听页面隐藏
 
  },
  onUnload:function(){
    // 生命周期函数--监听页面卸载
  },
  onPullDownRefresh: function() {
    // 页面相关事件处理函数--监听用户下拉动作
   
  },
  onReachBottom: function() {
    // 页面上拉触底事件的处理函数
  
  },
  /**
   * 
   */
  getDataList: function (type, flag) {
    if (type == 0) {
      this.getExchangeList(flag)
    } else if (type == 1) {
      this.getDoingExchangeList(flag)
    } else {
      this.getFinishedExchangeList(flag)
    }
  },

  /**
   * 获取可售后列表列表
   */
  getExchangeList: function (flag) {
    let token = wx.getStorageSync('token')
    let that = this

    if (flag == LOWER) {
      this.setData({
        loading: true,
      })
    }

    wx.request({
      url: app.apiUrl('/order/getexchangelist'),
      method: 'GET',
      header: {
        "Authorization": 'Bearer ' + token
      },
      data: {
        page: that.data.offset,
        page_size: that.data.limit
      },
      success: function (res) {
        console.log('getexchangelist')
        console.log(res.data)
        let listTemp = []
        let loadingComplete = false
        if (res.data.code > app.globalData.successCode) {

          if (res.data.code != 19001) {
            wx.showToast({
              title: res.data.error_message ? res.data.error_message : app.globalData.unknownError,
              icon: 'none',
              duration: 2000
            })    
          } else {
            loadingComplete = true
          }

          that.setData({
            orderList: [],
            loaded: true,
          })
          
        } else if (res.data.code == app.globalData.successCode) {
          app.login(that, 1)
        } else {
          if (!(res.data.data.length == that.data.limit)) {
            loadingComplete = true
          }

          listTemp = res.data.data
        
          that.setData({
            productList: flag == LOWER ? that.data.productList.concat(listTemp) : listTemp,
            loadingComplete: loadingComplete
          })
        }

        that.setData({
          loadingComplete: loadingComplete
        })
  
      },
      fail: function (res) {
        that.setData({
          noConnect: true
        })
        // wx.showModal({
        //   title: '提示',
        //   content: '网络请求异常',
        //   showCancel: false
        // })
      },
      complete: function (res) {
        that.setData({
          loaded: true
        })
        if (flag == LOWER) {
          that.setData({
            loading: false,
          })
        } else if (flag == PULLDOWN) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
        }
      }
    })
  },

  /**
  * 获取处理中的列表
  */
  getDoingExchangeList: function (flag) {
    let token = wx.getStorageSync('token')
    let that = this

    if (flag == LOWER) {
      this.setData({
        loading: true,
      })
    }

    wx.request({
      url: app.apiUrl('/order/getpendingexchangelist'),
      method: 'GET',
      header: {
        "Authorization": 'Bearer ' + token
      },
      data: {
        page: that.data.offset,
        page_size: that.data.limit
      },
      success: function (res) {
        console.log('getdoingexchangelist')
        console.log(res.data)
        let listTemp = []
        let loadingComplete = false
        if (res.data.code > app.globalData.successCode) {

          if (res.data.code != 19001) {
            wx.showToast({
              title: res.data.error_message ? res.data.error_message : app.globalData.unknownError,
              icon: 'none',
              duration: 2000
            })
          } else {
            loadingComplete = true
          }

          that.setData({
            orderList: [],
            loaded: true,
          })

        } else if (res.data.code == app.globalData.successCode) {
          app.login(that, 1)
        } else {
          if (!(res.data.data.length == that.data.limit)) {
            loadingComplete = true
          }

          listTemp = res.data.data

          that.setData({
            productList: flag == LOWER ? that.data.productList.concat(listTemp) : listTemp,
            loadingComplete: loadingComplete
          })
        }

        that.setData({
          loadingComplete: loadingComplete
        })

      },
      fail: function (res) {
        that.setData({
          noConnect: true
        })
       
      },
      complete: function (res) {
        that.setData({
          loaded: true
        })
        if (flag == LOWER) {
          that.setData({
            loading: false,
          })
        } else if (flag == PULLDOWN) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
        }
      }
    })
  },  

  /**
 * 获取已完成的列表
 */
  getFinishedExchangeList: function (flag) {
    let token = wx.getStorageSync('token')
    let that = this

    if (flag == LOWER) {
      this.setData({
        loading: true,
      })
    }

    wx.request({
      url: app.apiUrl('/order/getalreadyexchangelist'),
      method: 'GET',
      header: {
        "Authorization": 'Bearer ' + token
      },
      data: {
        page: that.data.offset,
        page_size: that.data.limit
      },
      success: function (res) {
        console.log('getFinishedExchangeList')
        console.log(res.data)
        let listTemp = []
        let loadingComplete = false
        if (res.data.code > app.globalData.successCode) {

          if (res.data.code != 19001) {
            wx.showToast({
              title: res.data.error_message ? res.data.error_message : app.globalData.unknownError,
              icon: 'none',
              duration: 2000
            })
          } else {
            loadingComplete = true
          }

          that.setData({
            orderList: [],
            loaded: true,
          })

        } else if (res.data.code == app.globalData.successCode) {
          app.login(that, 1)
        } else {
          if (!(res.data.data.length == that.data.limit)) {
            loadingComplete = true
          }

          listTemp = res.data.data

          that.setData({
            productList: flag == LOWER ? that.data.productList.concat(listTemp) : listTemp,
            loadingComplete: loadingComplete
          })
        }

        that.setData({
          loadingComplete: loadingComplete
        })

      },
      fail: function (res) {
        that.setData({
          noConnect: true
        })

      },
      complete: function (res) {
        that.setData({
          loaded: true
        })
        if (flag == LOWER) {
          that.setData({
            loading: false,
          })
        } else if (flag == PULLDOWN) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
        }
      }
    })
  },

  /**
  * 下拉刷新  
  */
  onPullDownRefresh: function () {
    this.data.offset = 1
    this.setData({
      loading: false,
      loadingComplete: false,
      loaded: false,
      noConnect: false,

    })
    this.data.list = []
    wx.showNavigationBarLoading()
    this.getDataList(this.data.currentType, PULLDOWN)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("toLower" + this.data.loadingComplete)
    this.setData({
      loading: this.data.loading,
      loadingComplete: this.data.loadingComplete
    })
    if (!this.data.loadingComplete) {
      this.data.offset += 1
      this.getDataList(this.data.currentType, LOWER)
    }
  },

  toServiceTap: function (e) {
    let index = e.currentTarget.dataset.index
    wx.navigateTo({
      url: "/pages/service/index?product=" + JSON.stringify(this.data.productList[index])
    })
  }

})
